<template>
  <div class="app-container">
    <el-row class="header">
      <el-col class="asd" :span="18">
        <el-input
          clearable
          class="title"
          v-model="data.params.topic"
          placeholder="主题名称"
          style="width: 300px; margin-right: 15px"
        />
        <popup-tree-input
          :is-inner="true"
          placeholder="点击选择所属公司"
          :props="data.deptTreeProps"
          :data="store.state.system.orgList"
          :prop="data.params.parentName"
          :current-change-handle="handleOrgList"
        />
        <el-select class="typeList" clearable v-model="data.params.type" placeholder="资讯类型">
          <el-option
            v-for="item in store.state.system.allSystemDict.infoDict.XTGL_NEWS_TYPE"
            :key="item.itemValue"
            :label="item.itemText"
            :value="item.itemValue"
          />
        </el-select>
        <el-button class="ordinary" @click="search()">搜索</el-button>
      </el-col>
      <el-col :span="6" align="right">
        <el-button v-limit="'XTGL:ZXGL:DELETE'" type="danger" @click="handleDel()"
          >批量删除</el-button
        >
        <el-button v-limit="'XTGL:ZXGL:ADD'" type="primary" @click="handleAdd()">新增</el-button>
      </el-col>
    </el-row>

    <Table
      :loading="data.isLoading"
      :tableArr="data.allTableData"
      :columns="data.columns"
      :page-size="data.params.size"
      :page-num="data.params.current"
      @selectList="getCurrentList"
    >
      <template #status>
        <el-table-column label="状态" width="150">
          <template #default="{ row }" class="delFlag">
            <el-switch
              v-model="row.status"
              active-text="正常"
              inactive-text="关闭"
              active-value="1"
              inactive-value="0"
              @change="swicthEnable(row)"
            />
          </template>
        </el-table-column>
      </template>
      <template #operate="{ row }">
        <el-button v-limit="'XTGL:ZXGL:EDIT'" class="ordinary" @click="handleEdit(row)"
          >编辑</el-button
        >
      </template>
    </Table>
    <div class="example-pagination-block">
      <el-pagination
        background
        layout="prev, pager, next ,total,sizes,jumper"
        :total="data.total"
        :page-size="data.params.size"
        v-model:current-page="data.params.current"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <el-dialog v-model="data.editDialog" width="60%" title="基本信息">
      <Information ref="formRef" v-if="data.editDialog"></Information>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.editDialog = false">关闭</el-button>
          <el-button type="primary" @click="handleSave()">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { reactive, ref } from 'vue'
import tableData from './tableHeader.json'
import Information from './information.vue'
import PopupTreeInput from '@/views/system/components/PopupTreeInput/index.vue'

import { addInfo, getInfoList, updateInfo, delBatchInfo, getInfoDict } from '../api/info'
import Table from '../components/table2/table.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex'
import { getOrgTree } from '../api/org'

export default {
  components: {
    Table,
    Information,
    PopupTreeInput,
  },
  setup() {
    const store = useStore()
    let data = reactive({
      params: {
        topic: '',
        type: '',
        parentName: '',
        current: 1,
        size: 10,
      },
      total: 0,
      isLoading: false,
      allTableData: [],
      columns: tableData.platformTableHeader,
      ids: [],
      currentList: [],
      currentHandle: 1,
      editDialog: false,
      deptTreeProps: {
        label: 'name',
        children: 'children',
      },
    })
    const formRef = ref()
    const getCurrentList = (currentList) => {
      console.log(currentList, 'getCurrentList')
      data.currentList = currentList
      data.ids = currentList.map((item) => {
        return item.id
      })
    }
    const search = () => {
      data.params.current = 1
      onSearch()
    }
    const onSearch = async () => {
      data.isLoading = true
      getInfoList(data.params).then((res) => {
        data.isLoading = false
        data.allTableData = res.data?.data?.records.map(item=>{
          item.createTime = item.createTime.slice(0,10)
          return item
        })
        data.total = res.data?.data?.total
      })
    }
    onSearch()
    //所属单位修改
    const handleOrgList = (item) => {
      console.log(item, 'handleOrgList')
      data.params.parentName = item.name
    }
    //改变页码
    const handleCurrentChange = (e) => {
      console.log(e, 'handleCurrentChange')
      data.params.current = e
      onSearch()
    }
    //改变页数限制
    const handleSizeChange = (e) => {
      console.log(e, 'handleSizeChange')
      data.params.size = e
      onSearch()
    }
    const swicthEnable = (row) => {
      console.log(row, 'swicthEnable')
      updateInfo({
        id:row.id,
        status:row.status
      }).then((res) => {
        if (res.data.code == 200) {
          // onSearch()
          ElMessage({
            message: '操作成功',
            type: 'success',
          })
        }
      })
    }
    const handleAdd = () => {
      data.editDialog = true
      data.currentHandle = 0
    }
    const handleEdit = (row) => {
      data.editDialog = true
      data.currentHandle = 1
      console.log(row, 'handleEdit')
      for(let key in store.state.system.newsForm){
        store.state.system.newsForm[key] = row[key]
      }
    }
    const handleDel = () => {
      if (data.ids.length) {
        ElMessageBox.confirm('您确认删除所选中数据吗？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
          callback: (action) => {
            if (action == 'confirm') {
              delBatchInfo(data.ids).then((res) => {
                console.log(res)
                onSearch()
                ElMessage({
                  message: '操作成功',
                  type: 'success',
                })
              })
            }
          },
        })
      } else {
        ElMessage({
          message: '请选择',
          type: 'warning',
        })
      }
    }
    const handleSave = () => {
      let params = formRef.value.submitForm()
      if (params) {
        if (data.currentHandle) {
          updateInfo(store.state.system.newsForm).then((res) => {
            if (res.data.code == 200) {
              data.editDialog = false
              onSearch()
              ElMessage({
                message: '操作成功',
                type: 'success',
              })
            }
          })
        } else {
          addInfo(store.state.system.newsForm).then((res) => {
            if (res.data.code == 200) {
              data.editDialog = false
              onSearch()
              ElMessage({
                message: '操作成功',
                type: 'success',
              })
            }
          })
        }
      }
    }
    if (Object.keys(store.state.system.allSystemDict.infoDict).length == 0) {
      getInfoDict().then((res) => {
        store.state.system.allSystemDict.infoDict = res.data?.data
        console.log(store.state.system.allSystemDict.infoDict, 'getInfoDict')
      })
    }
    if (store.state.system.orgList.length == 0) {
      getOrgTree().then((res) => {
        store.state.system.orgList = res.data.data
      })
    }
    return {
      data,
      store,
      formRef,
      onSearch,
      handleOrgList,
      getCurrentList,
      swicthEnable,
      handleCurrentChange,
      handleSizeChange,
      handleEdit,
      handleDel,
      handleAdd,
      handleSave,
      search,
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
}
.title {
  width: 200px !important;
}
.typeList {
  margin-left: 15px;
}
</style>
